package com.sanley.coronavirus.controller;

import com.github.pagehelper.PageInfo;
import com.sanley.coronavirus.common.api.CommonResult;
import com.sanley.coronavirus.entity.Notification;
import com.sanley.coronavirus.service.NotificationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.List;

/**
 * @Author: ming
 * @create: 2022-11-16 13:44
 * @program: coronavirus
 */
@Controller
@RequestMapping("/notification")
@Api(value = "NotificationController", description = "通知管理")
public class NotificationController {

    @Autowired
    private NotificationService notificationService;


    @GetMapping("/list")
    @ResponseBody
    @ApiOperation("分页获取通知")
    public CommonResult list(@RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "size", defaultValue = "5") int size) {
        List<Notification> notifications = notificationService.queryNotification(page, size);
        PageInfo<Notification> pageInfo = new PageInfo<>(notifications);
        return CommonResult.success(pageInfo);
    }

    @GetMapping("/getById")
    @ResponseBody
    @ApiOperation("根据id查找通知")
    public CommonResult getNotificationById(@RequestParam(value = "id") int id) {
        Notification notification = notificationService.getNotificationById(id);
        return CommonResult.success(notification);
    }

    @ApiOperation("根据标题查找通知")
    @GetMapping("/getByTittle")
    @ResponseBody
    public CommonResult getNotificationByTittle(@RequestParam(value = "tittle") String tittle,
                                                @RequestParam(value = "page", defaultValue = "1") int page,
                                                @RequestParam(value = "size", defaultValue = "5") int size) {
        List<Notification> notification = notificationService.getNotificationByTittle(tittle, page, size);
        PageInfo<Notification> pageInfo = new PageInfo<>(notification);
        return CommonResult.success(pageInfo);
    }

    @ApiOperation("删除通知")
    @PostMapping("/delete")
    @ResponseBody
    public CommonResult delete(@RequestParam(value = "id", required = true) Integer id) {
        int flag = notificationService.delete(id);
        if (flag == 1) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }

    @PostMapping("/add")
    @ApiOperation("添加通知")
    @ResponseBody
    public CommonResult add(@RequestParam(value = "tittle") String tittle, @RequestParam(value = "content") String content, Principal principal) {
        String username = principal.getName();
        Notification notification = new Notification().setTittle(tittle).setContent(content);
        int i = notificationService.addNotification(notification, username);
        if (i == 1) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }

    @PostMapping("/update")
    @ApiOperation("修改通知")
    @ResponseBody
    public CommonResult update(@RequestParam(value = "id") Integer id, @RequestParam(value = "tittle") String tittle, @RequestParam(value = "content") String content) {

        Notification notification = new Notification().setId(id).setTittle(tittle).setContent(content);
        int i = notificationService.updateNotification(notification);
        if (i == 1) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }
}
